package syscontrollers

import (
	"encoding/json"
	"myadmin/sys/syscontrollers/syscommon"
	"myadmin/sys/sysmodels"
	"strconv"
)

type SysRole struct {
	syscommon.BaseController
}

func (c *SysRole) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("Get", c.Get)
	c.Mapping("Delete", c.Delete)
	c.Mapping("Put", c.Put)

}

//@Title 新建角色
//@Description 新建角色
//@Param rolename form string true 角色标识
//@Param name json string true 角色名称
//@Param describe form string true 角色说明
//@Param status form bool false 是否有效
//@Param authlist form list false 权限标识list
//@success 200 {json} 权限json数据
//@Failure 500 {json} 创建失败
//@router /role [post]
func (this *SysRole) Post() {
	var err error
	mrole := sysmodels.NewRole()
	err = this.ParseForm(mrole)

	if err != nil {
		this.JsonRep500x("请求数据解析失败！", err)
		return
	}
	err = mrole.Add()
	if err != nil {
		this.JsonRep500x(err.Error(), err)
		return
	}
	this.JsonRep200("添加成功")
}

//@Title 查询角色
//@Description 查询角色
//@Param rolename url string false 角色标识
//@Param page url int false 页数
//@Param limit url int false 当前条数
//@success 200 {json} 权限json数据
//@Failure 500 {json} 创建失败
//@router /role [get]
func (this *SysRole) Get() {
	role := sysmodels.NewRole()
	page := this.GetString("page")
	p := this.S2i(page, 1)
	limit := this.GetString("limit")
	l := this.S2i(limit, 20)
	rolename := this.GetString("rolename")
	role.RoleName = rolename
	roles, cnt := role.Query(l, p)
	this.JsonReqStdS(200, int(cnt), l, p, roles)

}

//@Title 删除角色
//@Description 删除角色
//@Param id url string true 角色id
//@success 200 {json} 权限json数据
//@Failure 500 {json} 创建失败
//@router /role/:id [delete]
func (this *SysRole) Delete() {
	_id := this.Ctx.Input.Param(":id")
	id, err := strconv.Atoi(_id)
	if err != nil {
		this.JsonRep500("无效id")
		return
	}

	var role = sysmodels.NewRole()
	role.Id = id
	_, err = role.DeleteById()
	if err != nil {
		this.JsonRep500("删除失败")
		return
	}
	this.Ok204()
}

//@Title 修改角色
//@Description 修改角色
//@Param rolename form string false 角色标识
//@Param name form string false 角色名
//@Param rolename form string false 角色描述
//@Param authlist json string false 权限
//@success 200 {json} 权限json数据
//@Failure 500 {json} 创建失败
//@router /role/:id [put]
func (this *SysRole) Put() {
	_id := this.Ctx.Input.Param(":id")
	id, err := strconv.Atoi(_id)

	m := this.Input()
	keys := make([]string, 0, len(m))
	var authlist []string
	for k,v := range m {
		keys = append(keys, k)
		if k=="authlist"{
			tmp:=[]interface{}{}
			json.Unmarshal([]byte(v[0]),&tmp)
			_auth:=sysmodels.NewSysAuth()
			authobj,_:=_auth.GetByListId(tmp)
			for _,av:=range *authobj{
				authlist = append(authlist,av.AuthName)
			}
		}
	}
	role := sysmodels.NewRole()
	err = this.ParseForm(role)
	if err != nil {
		this.JsonRep500x("更新失败,上传数据格式不正确", err)
		return
	}
	if len(authlist)>0{
		role.AuthList = authlist
	}
	if err != nil {
		this.JsonRep500x("无效id", err)
	}

	role.Id = id
	err = role.Update(keys)
	if err != nil {
		this.JsonRep500(err.Error())
		return
	}
	this.JsonRep200("更新成功")
}
